﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.WebModel;
using TravelLink.DAL;

namespace TravelLink.WebApp.UserControl.Search {
    public partial class UC_CompanyPosition_Search : ControlBase {


        private int _pageSize = 10;
        public int PageSize { get { return _pageSize; } set { _pageSize = value; } }


        public string ISTVal { get; set; }
        public string ISTName { get; set; }


        protected void Page_Load(object sender, EventArgs e) {

            ISTVal = string.Empty;
            ISTName = string.Empty;


            UC_Pager1.PageSize = PageSize;
            int currentPage = 1;
            int.TryParse(HttpContext.Current.Request.QueryString["P"], out currentPage);
            if (currentPage < 1) {
                currentPage = 1;
            }
            UC_Pager1.PageIndex = currentPage;
            UC_Pager1.JumpUrl = this.GetPageJumpUrl();

            var dataList = this.DB.TL_Company_Position
                .Where(u => !u.Status.HasValue || (u.Status.HasValue && u.Status.Value != Enum_PositionStatus.Stop));
            if (DT > 0) {
                DateTime begin = DateTime.Now;
                DateTime end = DateTime.Now;
                WebHelper.GetSearchDate(DT, out begin, out end);

                dataList = dataList.Where(u => u.PublishDate.Value.Date >= begin && u.PublishDate.Value.Date <= end);
            }
            if (PT > 0) {
                dataList = dataList.Where(u => u.PositionType == PT);
            }
            if (PL > 0) {
                dataList = dataList.Where(u => u.PositionLevel == PL);
            }
            if (IST.Length > 0) {
                dataList = dataList.Where(u => u.TL_Company.TL_Company_Industry.FirstOrDefault(p => IST.Contains(p.IndustryId)) != null);
            }
            if (CT > 0) {
                dataList = dataList.Where(u => u.WorkInCityId == CT);
            }
            if (!string.IsNullOrEmpty(CPY)) {
                Guid cpyCode = Guid.Empty;
                if (Guid.TryParse(CPY, out cpyCode)) {
                    dataList = dataList.Where(u => u.CompanyCode == cpyCode);
                }
            }
            int total = dataList.Count();
            UC_Pager1.NoteTotal = total;
            var source = dataList.OrderByDescending(u => u.PublishDate).Skip((currentPage - 1) * _pageSize).Take(_pageSize).ToList();
            //dataList[0].TL_City.CName
            this.rptDataSource.DataSource = source;
            this.rptDataSource.DataBind();


            this.drpPositionLv.DataSource = this.DB.TL_PositionLevel;
            this.drpPositionLv.DataTextField = "Name";
            this.drpPositionLv.DataValueField = "Idx";
            this.drpPositionLv.DataBind();
            this.drpPositionLv.Items.Insert(0, new ListItem("--不限--", "-1"));
            if (this.DB.TL_PositionLevel.Where(u => u.Idx == PL).FirstOrDefault() != null) {
                drpPositionLv.SelectedValue = PL.ToString();
            }


            this.drpPositionJobType.DataSource = this.DB.TL_PositionType;
            this.drpPositionJobType.DataTextField = "Name";
            this.drpPositionJobType.DataValueField = "Idx";
            this.drpPositionJobType.DataBind();
            this.drpPositionJobType.Items.Insert(0, new ListItem("--不限--", "-1"));
            if (this.DB.TL_PositionType.Where(u => u.Idx == PT).FirstOrDefault() != null) {
                drpPositionJobType.SelectedValue = PT.ToString();
            }

            if (IST.Length > 0) {
                var istEntity = this.DB.TL_IndustryType2.Where(u => IST.Contains(u.Idx));
                if (istEntity != null) {
                    this.ISTVal = string.Join(",", IST);
                    this.ISTName = string.Join(",", istEntity.Select(u => u.Name));      //istEntity.Name;
                }
            }
        }


        protected string BindCity(object dataItem) {
            if (dataItem is TL_Company_Position) {
                var source = (dataItem as TL_Company_Position);
                if (source != null && source.TL_City != null) {
                    return source.TL_City.CName;
                }
                return string.Empty;
            }
            return string.Empty;
        }
    }
}